package edu.scau.hibernate.service;

import java.util.List;

import edu.scau.hibernate.dao.UserDao;
import edu.scau.hibernate.dao.UserDaoImpl;
import edu.scau.hibernate.model.User;


public class UserServiceImpl implements UserService{

	private UserDao userImpl=null;
	public UserServiceImpl() throws Exception{
		super();
		userImpl =new UserDaoImpl();
	}
	@Override
	public boolean deleteUser(User user) throws Exception {
		return userImpl.delete(user);
	}
	
	@Override
	public boolean updateUser(User user) throws Exception {
		return userImpl.update(user);
	}


	@Override
	public User findUserByUserName(String userName) throws Exception {
		return userImpl.findByUserName(userName);
	}
	
	@Override
	public List<User> findAllUser() throws Exception {
		return userImpl.findAll();
	}

	@Override
	public boolean addUser(User user) throws Exception {
		
		if(userImpl.findByUserName(user.getUserName())==null)   //用户名不存在
		    return userImpl.save(user);     //用户名不存在时注册该用户
		else
			return false;
	}

	@Override
	public boolean loginCheck(String userName,String password) throws Exception {
		String name=userImpl.findByUserName(userName).getUserName();
		String pass=userImpl.findByUserName(userName).getPassword();
		if(userName.equals(name)&&password.equals(pass)) 
              return true;
		return false;//用户存在且密码正确
		
	}
	
}
